﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace ComeSeeThis_MVC2.Models
{
    public enum Scale
    {
        Quarter = 4,
        Fifth = 5,
        Eighth = 8,
        Tenth = 10,
        Twelvth = 12,
        Sixteenth = 16,
        Eighteenth = 18,
        TwentyFourth = 24
    }

    //public enum TrackSize
    //{
    //    Small, Medium, Large
    //}

    public enum ProductType
    {
        All, Motor, Vehicle, Tire
    }

    public class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int) Math.Ceiling(TotalCount/(double) PageSize);

            AddRange(source.Skip(PageIndex*PageSize).Take(PageSize));
        }

        public bool HasPreviousPage { get { return (PageIndex > 0); } }

        public bool HasNextPage { get { return (PageIndex + 1 < TotalPages); } }
    }

    // 
    // Author: Bob Maes <bob.maes@euhm.be> 
    // 
    public static class EnumExtensions
    {
        /// <summary> 
        /// Gets all items for an enum value, with a a param. 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="value">The value.</param> 
        /// <returns></returns> 
        public static IEnumerable<T> GetAllItems<T>(this Enum value)
        {
            foreach (object item in Enum.GetValues(typeof(T)))
            {
                yield return (T)item;
            }
        }

        /// <summary> 
        /// Gets all items for an enum type. 
        /// </summary> 
        /// <typeparam name="T"></typeparam>
        /// <returns></returns> 
        public static IEnumerable<T> GetAllItems<T>() where T : struct
        {
            return Enum.GetValues(typeof(T)).Cast<T>();
        }

        /// <summary> 
        /// Gets all combined items from an enum value. 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="value">The value.</param> 
        /// <returns></returns> 
        public static IEnumerable<T> GetAllSelectedItems<T>(this Enum value)
        {
            var valueAsInt = Convert.ToInt32(value, CultureInfo.InvariantCulture);

            return from object item in Enum.GetValues(typeof(T)) let itemAsInt = Convert.ToInt32(item, CultureInfo.InvariantCulture) where itemAsInt == (valueAsInt & itemAsInt) select (T)item;
        }

        /// <summary> 
        /// Determines whether the enum value contains a specific value. 
        /// </summary> 
        /// <param name="value">The value.</param> 
        /// <param name="request">The request.</param> 
        /// <returns> 
        ///     <c>true</c> if value contains the specified value; otherwise, <c>false</c>. 
        /// </returns> 
        public static bool Contains<T>(this Enum value, T request)
        {
            var valueAsInt = Convert.ToInt32(value, CultureInfo.InvariantCulture);
            var requestAsInt = Convert.ToInt32(request, CultureInfo.InvariantCulture);

            return requestAsInt == (valueAsInt & requestAsInt);
        }
    }

}